import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_aliplayer/flutter_aliplayer.dart';
import 'package:projectfluttershengyihuatong/courseDetail/playermanger.dart';

import '../tool/player/config.dart';
class PlayerView extends StatefulWidget {
  PlayerView({super.key, this.alivid});
  String? alivid;

  @override
  State<PlayerView> createState() => _PlayerViewState();
}

class _PlayerViewState extends State<PlayerView> {




  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, constraints) {
      return SizedBox(
        width: constraints.maxWidth,
        height: constraints.maxHeight,
        child: Stack(
          children: [
            AliPlayerView(
              onCreated: onViewPlayerCreated,
              x: 0,
              y: 0,
              width: constraints.maxWidth,
              height: constraints.maxHeight,
              aliPlayerViewType: AliPlayerViewTypeForAndroid.surfaceview,
            ),

          ],
        ),
      );
    });
  }

  late PlayerManager manager = PlayerManager(ModeType.AUTH);


  void onViewPlayerCreated(viewId) async {
    manager.aliPlayerViewId = viewId;
    await manager.aliplayer.setPlayerView(viewId);
    final map = await manager.aliplayer.getConfig() as Map<Object?, Object?>;
    map["mReferrer"] = "https://app.syht.xyz";
    manager.aliplayer.setConfig(map);
    FlutterAliplayer.createVidPlayerConfigGenerator();
    await manager.generatePlayConfigGen();
    switch (manager.playMode) {
      case ModeType.URL:
        final map = await manager.requestAuth();
        // manager.aliplayer.setUrl(map[DataSourceRelated.URL_KEY]);
        break;
      case ModeType.STS:
      // FlutterAliplayer.generatePlayerConfig().then((value) {
      //
      // });

      // manager.aliplayer.setVidSts(
      //     vid: _dataSourceMap[DataSourceRelated.VID_KEY],
      //     region: _dataSourceMap[DataSourceRelated.REGION_KEY],
      //     accessKeyId: _dataSourceMap[DataSourceRelated.ACCESSKEYID_KEY],
      //     accessKeySecret:
      //     _dataSourceMap[DataSourceRelated.ACCESSKEYSECRET_KEY],
      //     securityToken:
      //     _dataSourceMap[DataSourceRelated.SECURITYTOKEN_KEY],
      //     definitionList: _dataSourceMap[DataSourceRelated.DEFINITION_LIST]);

        break;
      case ModeType.AUTH:
        Map<String, String> map = {};
        if (widget.alivid != null) {
          map = await manager.requestAuthByAlivid(widget.alivid!);
        }else {
          map = await manager.requestAuth();
        }
        manager.aliplayer.setVidAuth(
          vid: map[DataSourceRelated.VID_KEY],
          region: map[DataSourceRelated.REGION_KEY],
          playAuth: map[DataSourceRelated.PLAYAUTH_KEY],
        );
        manager.aliplayer.setLoop(true);
        manager.aliplayer.prepare();


        break;
      case ModeType.MPS:
      // manager.aliplayer.setVidMps(_dataSourceMap);
        break;
      default:
    }
  }

}




